using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PawnAnchor : MonoBehaviour
{
    public Pawn Pawn;
    public CardPosition AnchorPosition = null;

    public void ReturnPosition()
    {
        if (AnchorPosition == null)
            return;
        transform.position = AnchorPosition.transform.position;
        if (AnchorPosition.TryGetComponent<GridNode>(out var node))
        {

            // 清楚之前占据的Grid的pawn数据
            if (Pawn.gridNode?.Pawn == Pawn)
            {
                Pawn.gridNode.Pawn = null;
            }

            // 写入现在占据的Grid的pawn数据
            node.Pawn = Pawn;
            Pawn.gridNode = node;

        }
    }

    public bool SetAnchor(CardPosition position)
    {
        if (!position.IfAvailable)
            return false;
        AnchorPosition?.Clear();
        AnchorPosition = position;
        position.SetPawn(Pawn);
        transform.position = AnchorPosition.transform.position;
        return true;
    }

    public bool SetAnchorForce(CardPosition position)
    {
        AnchorPosition = position;
        position.SetPawn(Pawn);
        transform.position = AnchorPosition.transform.position;
        return true;
    }

    bool ifDraging;
    private void Update()
    {
        if (Pawn.ifHero)
            return;
        if (ifDraging)
            MousePoint.ifDraging = true;
        // 判断当前状态是否为palce
        if (!LevelManager.instance.ifCanDrag)
            return;
        if (!Pawn.campIsSelf)
            return;
        if (!ifDraging)
        {
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                if (MousePoint.CurrentPawn == Pawn)
                {
                    ifDraging = true;
                }
            }
        }

        if (ifDraging)
        {
            transform.position = MousePoint.MousePosition;

            if (Input.GetKeyUp(KeyCode.Mouse0))
            {
                // 看当前是否存在position
                if (MousePoint.CurrentCardPosition != null)
                {
                    var pos = MousePoint.CurrentCardPosition;

                    if (pos.transform.TryGetComponent<GridNode>(out var node))
                    {
                        if (node.layer >= 4)
                        {
                            ReturnPosition();
                            ifDraging = false;
                            return;
                        }
                        if (node.Pawn == null)
                        {
                            if (Pawn.gridNode?.Pawn == Pawn)
                            {
                                Pawn.gridNode.Pawn = null;
                            }
                            node.Pawn = Pawn;
                            Pawn.gridNode = node;
                            SetAnchor(pos);
                        }
                        else if (!node.Pawn.ifHero)
                        {


                            var targetPawn = node.Pawn;
                            var myNode = Pawn.gridNode;
                            var myPos = AnchorPosition;

                            // 己方占据node
                            node.Pawn = Pawn;
                            Pawn.gridNode = node;

                            // 对方占据node
                            if (myNode != null)
                                myNode.Pawn = targetPawn;
                            targetPawn.gridNode = myNode == null ? null : myNode;

                            // 改变双方anchor
                            SetAnchorForce(pos);
                            targetPawn.PawnAnchor.SetAnchorForce(myPos);

                        }
                        else if (node.Pawn.ifHero && GameManager.instance.ifPossessionMode)
                        {
                            var targetPawn = node.Pawn;

                            var myNode = Pawn.gridNode;
                            var myPos = AnchorPosition;

                            // 解绑自身
                            if (myNode != null)
                                myNode.Pawn = null;

                            myPos.SetPawn(null);

                            // 给予能力
                            targetPawn.FeatureMono.Features.Clear();
                            targetPawn.FeatureMono.Features.AddRange(Pawn.FeatureMono.Features);
                            targetPawn.Roots.RootsFeatures.AddRange(Pawn.Roots.RootsFeatures);

                            // 销毁自身
                            Destroy(gameObject);
                            return;

                        }
                    }
                    else
                    {
                        if (pos.Pawn != null && pos.Pawn != Pawn && !pos.Pawn.ifHero)
                        {
                            var targetPawn = pos.Pawn;
                            var myNode = Pawn.gridNode;
                            var myPos = AnchorPosition;

                            if (myNode != null)
                            {
                                targetPawn.gridNode = myNode;
                                myNode.Pawn = targetPawn;
                            }

                            SetAnchorForce(pos);
                            targetPawn.PawnAnchor.SetAnchorForce(myPos);
                        }
                        else
                        {
                            if (Pawn.gridNode?.Pawn == Pawn)
                            {
                                Pawn.gridNode.Pawn = null;
                            }
                            Pawn.gridNode = null;
                            SetAnchor(pos);
                        }
                    }

                }
                ReturnPosition();
                ifDraging = false;
                MousePoint.ifDraging = false;
            }
            if (Input.GetKeyDown(KeyCode.Mouse1))
            {
                MousePoint.ifDraging = false;
                ReturnPosition();
                ifDraging = false;
            }
        }
    }
}
